package org.mall.modules.promotion.controller.users;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.constant.CommonStatusEnum;
import org.mall.convert.promotion.admin.ProductRecommendConvert;
import org.mall.modules.pojo.product.bo.ProductSpuBO;
import org.mall.modules.pojo.promotion.bo.ProductRecommendBO;
import org.mall.modules.pojo.promotion.users.UsersProductRecommendVO;
import org.mall.modules.product.entities.CommoditySpu;
import org.mall.modules.product.service.ICommoditySpuService;
import org.mall.modules.product.service.ProductSpuService;
import org.mall.modules.promotion.service.ProductRecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users/product_recommend")
@Api("商品推荐模块")
public class UsersProductRecommendController {

   @Autowired
    private ProductRecommendService productRecommendService;
   @Autowired
    private ProductSpuService productSpuService;
   @Autowired
   private ICommoditySpuService commoditySpuService;

    @GetMapping("/list")
    @ApiOperation("获得所有 Banner 列表")
    public CommonResult<Map<Integer, Collection<UsersProductRecommendVO>>> list() {
        // 查询商品推荐列表
        List<ProductRecommendBO> productRecommends = productRecommendService.getProductRecommendList(
                null, CommonStatusEnum.ENABLE.getValue());
        // 获得商品集合
        List<CommoditySpu> spuList= commoditySpuService.getSpuList(productRecommends.stream().map(ProductRecommendBO::getProductSpuId).collect(Collectors.toList()));

        List<ProductSpuBO> spus = productSpuService.getProductSpuList(
                productRecommends.stream().map(ProductRecommendBO::getProductSpuId).collect(Collectors.toSet()));
        for (ProductSpuBO spuBO:spus){
            for (CommoditySpu spu:spuList){
                if (spu.getId().equals(spuBO.getId())){
                    spuBO.setSalesVolume(spu.getSalesVolume());
                    break;
                }
            }
        }
        //获取商铺推荐信息集合
        List<ProductRecommendBO> sorts=productRecommendService.getProductSortsofSpuid(spus.stream().map(ProductSpuBO::getId).collect(Collectors.toList()));
        for(int i=0;i<spus.size();i++){
            for (int j=0;j<sorts.size();j++){
                if(spus.get(i).getId().equals(sorts.get(j).getProductSpuId())){
                    spus.get(i).setSort(sorts.get(j).getSort());
                }
            }
        }
        Map<Long, ProductSpuBO> spuMap = spus.stream().collect(Collectors.toMap(ProductSpuBO::getId, account -> account));
        // 组合结果，返回
        Multimap<Integer, UsersProductRecommendVO> result = HashMultimap.create();
//        productRecommends.sort(Comparator.comparing(ProductRecommendBO::getSort)); // 排序，按照 sort 降序
        productRecommends.forEach(productRecommendBO -> result.put(productRecommendBO.getType(),
                ProductRecommendConvert.INSTANCE.convert(spuMap.get(productRecommendBO.getProductSpuId())))); // 将 ProductSpuBO 添加到 results 中
        Map<Integer, Collection<UsersProductRecommendVO>> re=new HashMap<>();
        re.put(1,result.asMap().get(1).stream().sorted(Comparator.comparing(UsersProductRecommendVO::getSort).reversed()).collect(Collectors.toList()));
        return CommonResult.success(re);
    }

}
